package com.heima.shop.service.impl;

import com.heima.shop.mapper.PartsTypeAttributeMapper;
import com.heima.shop.service.PartsTypeAttributeService;
import com.heima.common.exception.CustomException;
import com.heima.model.shop.dtos.PartsTypeAttributeDto;
import com.heima.model.shop.pojos.PartsTypeAttribute;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class PartsTypeAttributeServiceImpl extends ServiceImpl<PartsTypeAttributeMapper, PartsTypeAttribute> implements PartsTypeAttributeService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(PartsTypeAttributeDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<PartsTypeAttribute> wraaper = new LambdaQueryWrapper<>();

        // 属性名称
         if (StringUtils.isNotBlank(dto.getName())) {
            wraaper.like(PartsTypeAttribute::getName, dto.getName());
         }
        // 单位
         if (StringUtils.isNotBlank(dto.getCompany())) {
            wraaper.like(PartsTypeAttribute::getCompany, dto.getCompany());
         }
        // 可选项
         if (StringUtils.isNotBlank(dto.getOptions())) {
            wraaper.like(PartsTypeAttribute::getOptions, dto.getOptions());
         }
        // 默认选项
         if (StringUtils.isNotBlank(dto.getDetailOption())) {
            wraaper.like(PartsTypeAttribute::getDetailOption, dto.getDetailOption());
         }

        // 分类属性id
         if (dto.getId() != null) {
            wraaper.eq(PartsTypeAttribute::getId, dto.getId());
         }

        // 配件分类id
         if (dto.getPartsTypeId() != null) {
            wraaper.eq(PartsTypeAttribute::getPartsTypeId, dto.getPartsTypeId());
         }

        // 表单类型
         if (dto.getFormType() != null) {
            wraaper.eq(PartsTypeAttribute::getFormType, dto.getFormType());
         }

        // 是否搜索
         if (dto.getIsSearch() != null) {
            wraaper.eq(PartsTypeAttribute::getIsSearch, dto.getIsSearch());
         }

        // 是否必填
         if (dto.getIsMust() != null) {
            wraaper.eq(PartsTypeAttribute::getIsMust, dto.getIsMust());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param partsTypeAttribute
     * @return
     */
    @Override
    public ResponseResult insert(PartsTypeAttribute partsTypeAttribute) {
        //1 参数检查
        if (partsTypeAttribute == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(partsTypeAttribute);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(PartsTypeAttribute partsTypeAttribute) {

        //1.检查参数
        if(null == partsTypeAttribute){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(partsTypeAttribute);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
